<template>
  <div class="edit-container">
    <!-- 顶部 -->
    <van-nav-bar
      title="编辑资料"
      left-text="返回"
      left-arrow
      @click-left="$router.go(-1)"
    />

    <!-- 头像 -->
    <div class="avatar">
      <van-image
        width="120"
        height="120"
        fit="cover"
        round
        :src="userInfo.photo"
      />
      <van-uploader :after-read="afterRead" />
    </div>
    <van-cell-group>
      <van-cell
        title="名称"
        is-link
        :value="userInfo.name"
        @click="showDlogclick"
      />
      <van-cell
        title="性别"
        @click="showPopupclick"
        is-link
        :value="userInfo.gender ? '女' : '男'"
      />
      <van-cell
        title="生日"
        is-link
        @click="datePickerClick"
        :value="userInfo.birthday"
      />
    </van-cell-group>

    <!-- 编辑姓名弹窗 -->
    <van-dialog
      @confirm="editName"
      v-model="showDialog"
      title="编辑姓名"
      show-cancel-button
    >
      <van-field
        ref="nameFied"
        placeholder="请输入昵称"
        v-model="newName"
      ></van-field>
    </van-dialog>

    <!-- 编辑性别弹窗 -->
    <van-popup position="bottom" v-model="showPopup">
      <van-nav-bar
        title="修改性别"
        left-arrow
        @click-left="showPopup = false"
      />
      <van-cell title="男" is-link @click="editGender(0)" />
      <van-cell title="女" is-link @click="editGender(1)" />
    </van-popup>

    <!-- 编辑生日弹窗 -->
    <van-popup position="bottom" v-model="showDatepicker">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="showDatepicker = false"
        @confirm="updateBirthday"
      />
    </van-popup>

    <!--  -->
    <div class="cropper-box" v-show="showCropper">
      <vueCropper
        fixed
        autoCrop
        ref="cropper"
        :img="img"
        autoCropWidth="150"
        autoCropHeight="150"
      ></vueCropper>
      <van-button class="confirm" type="primary" @click="submitPhoto"
        >确定</van-button
      >
      <van-button class="cancel" type="primary" @click="showCropper = false"
        >取消</van-button
      >
    </div>
  </div>
</template>

<script>
//导入mapstate辅助函数
import { mapState } from 'vuex'
//导入请求接口
import { editUserInfo, editUserAvatar } from './user'
//导入moment.js
import * as moment from 'moment'
//裁切图片  VueCropper
import { VueCropper } from 'vue-cropper'
export default {
  name: 'edit',
  data () {
    return {
      showDialog: false, //编辑姓名弹窗
      newName: '', //姓名输入文本
      showPopup: false, //编辑性别弹窗
      showDatepicker: false,
      minDate: new Date(1949, 0, 1), //最小
      maxDate: new Date(), //最大
      currentDate: new Date(), //当前日期
      showCropper: false, //裁切窗口
      img: null //要裁切图片
    }
  },
  //使用裁切组件
  components: {
    VueCropper
  },
  methods: {
    //弹出编辑姓名框
    showDlogclick () {
      //点击时弹出
      this.showDialog = true
      //讲个人name数据渲染上去
      this.newName = this.userInfo.name

      //使用高级 定时器解决异步在同步后面执行的问题
      this.$nextTick(() => {
        //利用ref 获取光标
        this.$refs.nameFied.focus()
      })
    },
    //发起请求修改名称
    editName () {
      //调用接口
      //将当前输入框的数据传给接口的obj 发送请求
      editUserInfo({ name: this.newName }).then(res => {
    //    console.log(res)
        //请提示
        this.$toast.success('更新成功')
        //更新数据
        this.$store.commit('updateuserInfo', {
          ...this.userInfo, //先取出所有属性
          name: this.newName //覆盖name为修改的的数据
        })
      })
    },
    //弹出修改姓名框
    showPopupclick () {
      //点击时弹出
      this.showPopup = true
    },
    //发起请求修改性别
    editGender (gender) {
      editUserInfo(gender).then(res => {
       // console.log(res)

        //请提示
        this.$toast.success('修改成功')
        //关闭
        this.showPopup = false
        //更新数据
        this.$store.commit('updateuserInfo', {
          ...this.userInfo, //先取出所有属性
          gender //覆盖name为修改的的数据
        })
      })
    },
    // <!-- 点击生日 -->
    datePickerClick () {
      //显示弹框
      this.showDatepicker = true
      //把vuex中的字符串生成date对象
      let date = new Date(this.userInfo.birthday)
      //把日期对象给到currentDate
      this.currentDate = date
    },
    //发起请求修改生日
    updateBirthday () {
      //转换日期格式
      let birthday = moment(this.currentDate).format('YYYY-MM-DD')
      console.log(birthday)
      editUserInfo({ birthday }).then(res => {
       // console.log(res)
        //(1)弹窗提示
        this.$toast.success('更新成功!')
        //(2)更新vuex数据
        this.$store.commit('updateuserInfo', {
          ...this.userInfo,
          birthday: birthday
        })
        //(3)关闭弹窗
        this.showDatepicker = false
      })
    },
    //修改头像
    afterRead (file) {
      // 此时可以自行将文件上传至服务器
      //console.log(file)
      this.img = file.content
      this.showCropper = true
    },

    submitPhoto () {
      // 获取截图的blob数据
      this.$refs.cropper.getCropBlob(blob => {
        // console.log(blob)
        const fd = new FormData()
        fd.append('photo', blob)
        editUserAvatar(fd).then(res => {
          //(1)弹窗提示
          this.$toast.success('更新成功!')
          //(2)更新vuex数据
          this.$store.commit('updateuserInfo', {
            ...this.userInfo,
            photo: res.data.data.photo
          })
          //(3)关闭裁切窗口
          this.showCropper = false
        })
      })
    }
  },
  computed: {
    ...mapState(['userInfo'])
  }
}
</script>

<style lang="less">
.avatar {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 15px 0;
  .van-uploader {
    position: absolute;
    opacity: 0;
    width: 125px;
    height: 123px;
    .van-uploader__input {
      width: 130px;
      height: 130px;
    }
  }
}

.cropper-box {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  .confirm {
    position: absolute;
    right: 0;

    bottom: 0;
  }
  .cancel {
    position: absolute;
    left: 0;
    bottom: 0;
  }
}
</style>
